﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EBA.Contracts
{
    public static class Contract
    {
        public static void Requires(bool condition, string userMessage=null)
        {
            if(!condition)
            {
                throw new ContractFailException(userMessage??string.Empty);
            }
        }

        public static void Requires<TException>(bool condition,string userMessage=null) where TException:Exception
        {
            if (!condition)
            {
                var constructor = typeof(TException).GetConstructor(new Type[] { typeof(string) });

                throw constructor.Invoke(new object[]{userMessage??string.Empty}) as Exception;
            }
        }

        public static void Ensures(bool condition, string userMessage=null)
        {
            if (!condition)
            {
                throw new ContractFailException(userMessage??string.Empty);
            }
        }

        public static void Ensures<TException>(bool condition, string userMessage=null) where TException : Exception
        {
            if (!condition)
            {
                var constructor = typeof(TException).GetConstructor(new Type[] { typeof(string) });

                throw constructor.Invoke(new object[] { userMessage??string.Empty }) as Exception;
            }
        }
    }
}
